import absevnt from '../abseventsvc.js'
import {LGXEnums}  from '../../canvas/common/sysenum.js';
import lgxcom from '../../canvas/common/common.js'
import globalConst from '../../canvas/common/globalConst.js';
var global = globalConst.GlobalConst;
import { GlobalVar } from '../../canvas/common/globalvar.js';
import textinfo from '../../canvas/common/struct/basestruct/textinfo.js';
var startEditFlag = false;
var textKeyDown = false;
var txtinfo = null;
class EditTextSvc extends absevnt.IEventSvc {
    constructor() {
        super();
        startEditFlag = false;
        textKeyDown = false;

        console.log("EditTextSvc -- constructor");
    }

    act(e) {
        startEditFlag = true;
        console.log("EditTextSvc:xxx---------");
        console.log(e);
        window.mainctl.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
        let textAreaLeft = e.scope_left_wnd;
        let textAreaTop = e.scope_top_wnd;
        let textAreaWid = e.scope_right_wnd - e.scope_left_wnd;
        
        if (e.gid > 0) {
            var texttype = globalConst.GlobalConst.BaseUnitText;
            texttype = texttype << 16;
            txtinfo = window.mainctl.m_pGetDataINTF.getGraphInfo(texttype, e.gid);
            let mapBkColor = window.mainctl.m_pGetDataINTF.getCanvasBackColor();
            txtinfo.backColor = mapBkColor.toValue();
            txtinfo.color = mapBkColor.invert().toValue();
        }
        else {
            txtinfo = window.mainctl.m_pGetDataINTF.getNewTextInfo();
            txtinfo.exSetNewFlag = true;
        }
        let fontsize = txtinfo.fontsize * e.scaling;
        let originalTextAreaHgt = Math.abs(e.scope_bottom_wnd - e.scope_top_wnd) + fontsize;

        let textbox = document.getElementsByClassName('canvasTextBox')[0];
        

        textbox.style['z-index'] = 3;
        textbox.style.display = "block";
        textbox.style.left = `${textAreaLeft}px`;
        textbox.style.top = `${textAreaTop}px`;
        textbox.style.width = `${textAreaWid}px`;
        
        if (txtinfo.exSetNewFlag) {
            textbox.value = "";
        }
        else {
            textbox.value = txtinfo.sOrigContent;
        }
        let boxSize = this.calBoxSize(e, txtinfo.sOrigContent, fontsize);
        textbox.cols = boxSize.col;
        textbox.rows = boxSize.row;
        textbox.style.height = originalTextAreaHgt + 'px';

        textbox.style['font'] = fontsize + "px " + txtinfo.fontFamily;
        var lgxcolor = new lgxcom.LGXColor();
        lgxcolor.setColorByValue(txtinfo.color);
        textbox.style['color'] = lgxcolor.toHexString();

        lgxcolor.setColorByValue(txtinfo.backColor);
        textbox.style['background-color'] = lgxcolor.toHexString();
        textbox.focus();
        window.listenAllKeyEvent = false;
        textbox.addEventListener('input', (e) => {
            console.log(e);
            // if (e.inputType == "insertLineBreak"){
            //     textbox.blur();
            //     console.log(e);
            // }
            // else if (e.inputType == "insertText" && e.data == null){
            //     textbox.blur();
            // }
            textbox.style.height = fontsize + 'px';
            let height = e.target.scrollHeight;
            // if (height > originalTextAreaHgt)
            {
                textbox.style.height = height+'px';
            }
            textKeyDown = true;
            console.log("text area height:"+height);
            let textvalue = e.target.value;
            // console.log(textvalue);
        });

        let that = this;
        textbox.addEventListener('blur', () => {
            window.listenAllKeyEvent = true;
            if (!startEditFlag || !textKeyDown) {
                return;
            }
            
            startEditFlag = false;
            textKeyDown = false;
            let textvalue = textbox.value;
            console.log(textvalue);
            textbox.style.display = "none";
            textbox.style['z-index'] = 1;
            textbox.value = "";
            txtinfo.sFormatContent = textvalue;//格式化后，只有一行的文本内容
            txtinfo.sOrigContent = textvalue;//原始文本內容，可以帶換行符
            console.log("textbox.addEventListener('blur':" + textvalue + "   flag=" + startEditFlag);

            if (txtinfo.exSetNewFlag) {
                let left = this.getStylePosValue(textbox.style.left);
                let width = this.getStylePosValue(textbox.style.width);
                let top = this.getStylePosValue(textbox.style.top);
                let height = this.getStylePosValue(textbox.style.height);
                let cntx = left + width/2;
                let cnty = top + height/2;
                txtinfo.x = cntx;//文本左上角坐标点
                txtinfo.y = cnty;
                let pos = txtinfo.pos;
                pos.x = cntx;
                pos.y = cntx;
            }
            window.mainctl.m_pEditINTF.modifyGraphDataByStru(txtinfo);
        });
    }

    getStylePosValue(posDecs){
        let valStr = posDecs;
        let val = 0;
        if (valStr.indexOf('px') >= 0) {
            val = parseInt(valStr.substring(0, valStr.length - 2));
        }
        return val;
    }

    calBoxSize(e, textContent, fontsize) {
        let wid = e.scope_right_wnd - e.scope_left_wnd;
        let hgt = e.scope_bottom_wnd - e.scope_top_wnd;

        let maxCol = 1;
        let row = 1;
        if (textContent.length == 0) {
            maxCol = wid / fontsize;
            row = hgt / fontsize;
        }
        else {
            var words = textContent.split(global.NEWTXTLINEMARK);
            for (var n = 0; n < words.length; n++) {
                var textLine = words[n];
                maxCol = Math.max(maxCol, textLine.length);
            }
            row = n;
        }
        if (row < 1) {
            row = 1;
        }
        if (maxCol < 1) {
            maxCol = 1;
        }
        return { col: maxCol * 2, row };
    }

}

export default { EditTextSvc }